package org.light.utils;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Statement;
import org.light.domain.Util;
public class CORSFilterGenerator extends Util{
	public CORSFilterGenerator(){
		super();
		this.fileName = "CORSFilter.java";
		this.standardName = "CORSFilter";
	}
	
	public CORSFilterGenerator(String packageToken){
		super();
		this.packageToken = packageToken;
		this.fileName = "CORSFilter.java";
		this.standardName = "CORSFilter";
	}
	
	@Override
	public void setPackageToken(String packageToken){
		this.packageToken = packageToken;
	}
	
	@Override
	public String generateUtilString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package "+this.packageToken+".filter;"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import java.io.IOException;"));
		sList.add(new Statement(4000L,0,""));
		sList.add(new Statement(5000L,0,"import javax.servlet.Filter;"));
		sList.add(new Statement(6000L,0,"import javax.servlet.FilterChain;"));
		sList.add(new Statement(7000L,0,"import javax.servlet.FilterConfig;"));
		sList.add(new Statement(8000L,0,"import javax.servlet.ServletException;"));
		sList.add(new Statement(9000L,0,"import javax.servlet.ServletRequest;"));
		sList.add(new Statement(10000L,0,"import javax.servlet.ServletResponse;"));
		sList.add(new Statement(11000L,0,"import javax.servlet.http.HttpServletRequest;"));
		sList.add(new Statement(12000L,0,"import javax.servlet.http.HttpServletResponse;"));
		sList.add(new Statement(13000L,0,""));
		sList.add(new Statement(14000L,0,"import org.springframework.stereotype.Component;"));
		sList.add(new Statement(15000L,0,"import org.springframework.web.cors.CorsConfiguration;"));
		sList.add(new Statement(16000L,0,"import org.springframework.web.cors.CorsProcessor;"));
		sList.add(new Statement(17000L,0,"import org.springframework.web.cors.CorsUtils;"));
		sList.add(new Statement(18000L,0,"import org.springframework.web.cors.DefaultCorsProcessor;"));
		sList.add(new Statement(18500L,0,"import "+this.packageToken+".utils.MyConfigurationSource;"));
		sList.add(new Statement(19000L,0,""));
		sList.add(new Statement(20000L,0,"@Component"));
		sList.add(new Statement(21000L,0,"public class CORSFilter implements Filter {"));
		sList.add(new Statement(22000L,1,"private final MyConfigurationSource myconfigSource = new MyConfigurationSource();"));
		sList.add(new Statement(23000L,0,""));
		sList.add(new Statement(24000L,1,"private CorsProcessor processor = new DefaultCorsProcessor();"));
		sList.add(new Statement(25000L,0,""));
		sList.add(new Statement(26000L,1,"public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)"));
		sList.add(new Statement(27000L,3,"throws IOException, ServletException {"));
		sList.add(new Statement(28000L,2,"HttpServletRequest request = (HttpServletRequest) req;"));
		sList.add(new Statement(29000L,2,"HttpServletResponse response = (HttpServletResponse) res;"));
		sList.add(new Statement(30000L,2,"response.setHeader(\"Access-Control-Allow-Origin\", \"*\");"));
		sList.add(new Statement(31000L,2,"response.setHeader(\"Access-Control-Allow-Methods\", \"POST, GET, OPTIONS, DELETE\");"));
		sList.add(new Statement(32000L,2,"response.setHeader(\"Access-Control-Max-Age\", \"3600\");"));
		sList.add(new Statement(33000L,2,"response.setHeader(\"Access-Control-Allow-Headers\","));
		sList.add(new Statement(34000L,4,"\"x-token, content-type, X-Requested-With, XMLHttpRequest,Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With\");"));
		sList.add(new Statement(35000L,2,"response.setHeader(\"Access-Control-Exposed-Headers\","));
		sList.add(new Statement(36000L,4,"\"x-token, content-type, X-Requested-With, XMLHttpRequest,Referrer Policy:no-referrer-when-downgrade\");"));
		sList.add(new Statement(37000L,2,"if (CorsUtils.isCorsRequest(request)) {"));
		sList.add(new Statement(38000L,3,"CorsConfiguration corsConfiguration = this.myconfigSource.getCorsConfiguration(request);"));
		sList.add(new Statement(39000L,3,"if (corsConfiguration != null) {"));
		sList.add(new Statement(40000L,4,"boolean isValid = this.processor.processRequest(corsConfiguration, request, response);"));
		sList.add(new Statement(41000L,4,"if (!isValid || CorsUtils.isPreFlightRequest(request)) {"));
		sList.add(new Statement(42000L,5,"return;"));
		sList.add(new Statement(43000L,4,"}"));
		sList.add(new Statement(44000L,3,"}"));
		sList.add(new Statement(45000L,2,"}"));
		sList.add(new Statement(46000L,0,""));
		sList.add(new Statement(47000L,2,"chain.doFilter(request, response);"));
		sList.add(new Statement(48000L,1,"}"));
		sList.add(new Statement(49000L,0,""));
		sList.add(new Statement(50000L,1,"public void init(FilterConfig filterConfig) {"));
		sList.add(new Statement(51000L,1,"}"));
		sList.add(new Statement(52000L,0,""));
		sList.add(new Statement(53000L,1,"public void destroy() {"));
		sList.add(new Statement(54000L,1,"}"));
		sList.add(new Statement(55000L,0,"}"));
		
		return WriteableUtil.merge(sList).getContent();
	}
}